import 'package:flutter/material.dart';
import 'package:gpt_request_lib/dio_helper.dart';
import 'package:gpt_request_lib/repository/gpt_repository.dart';
import 'package:gpt_request_lib/postdata/gpt_message_post.dart';

import '../../chat_messge_page.dart';
import '../../data/message_data.dart';

class ChatProvider extends ChangeNotifier {
  // 按钮点击控制
  bool _isButtonEnable = false;

  // 聊天数据列表
  List<MessageData> _messageDataList = [];

  // 调用gptRepository.gptChatMessage()方法
  void getChatMessage({String msg = 'Hello!'}) {
    // 请求中不可点击
    _isButtonEnable = false;
    _messageDataList.insert(0,MessageData(
        isMe: true,
        message: msg,
        timeMillis: DateTime.now().microsecondsSinceEpoch.toString()));
    notifyListeners();
    print("123123123 $msg");
    GPTRepository(DioHelper.instance.dio)
        .gptChatMessage(
            GptMessagePost('gpt-3.5-turbo', [Messages('user', "$msg")]))
        .then((value) {
      // 请求结束
      _isButtonEnable = true;
      _messageDataList.insert(0,MessageData(
          isMe: false,
          message: value.choices[0].message.content,
          timeMillis: DateTime.now().microsecondsSinceEpoch.toString()));
      notifyListeners();
    });
  }

  bool get isButtonEnable => _isButtonEnable;


  set isButtonEnable(bool value) {
    _isButtonEnable = value;
    notifyListeners();
  }

  List<MessageData> get messageDataList => _messageDataList;
}
